﻿/*----------------------------------------------------------------
// Copyright (C) 2011 上海
// 版权所有。 
//
// 文件名：DataAccessSubsidyPaymentDepSummary
// 文件功能描述：补贴发放分部门汇总表数据访问层
//
// 
// 创建标识：2012-06-18 王冲
//
// 修改标识：
// 修改描述：
//
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbAdapter.sqldbAdapter;
using System.Data;
using System.Data.SqlClient;

namespace DAO.WaiGaoQiao
{
    public class DataAccessSubsidyPaymentDepSummary
    {
        /// <summary>
        /// 获取补贴发放部门的数量
        /// </summary>
        /// <returns></returns>
        public int GetListCount()
        {
            sqldb db = new sqldb();
            string query = "select count(*) from (select distinct(a.Dep_ID),a.deptName,Count(c.ID) as BTCount,SUM(c.ConMoney/100.00) as BTMoney from DeptView a " +
                           "left join V_PERSONNEL_REPORT b on a.Dep_ID=b.DEP_ID left join Con_Record c on b.PID=c.PID " +
                           "where c.DealMark in(select DepositTypeID from CON_DEPOSITTYPE where DepositName in('单位补贴')) " +
                           "group by a.Dep_ID,a.deptName) a1";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        public int GetListCounttWhere(string Condition)
        {
            sqldb db = new sqldb();
            string query = "select count(*) from (select distinct(a.Dep_ID),a.deptName,Count(c.ID) as BTCount,SUM(c.ConMoney/100.00) as BTMoney from DeptView a " +
                           "left join V_PERSONNEL_REPORT b on a.Dep_ID=b.DEP_ID left join Con_Record c on b.PID=c.PID " +
                           "where c.DealMark in(select DepositTypeID from CON_DEPOSITTYPE where DepositName in('单位补贴')) " +
                           Condition + " group by a.Dep_ID,a.deptName) a1";
            object rowcount = db.ExecuteScalar(GetConntionString.conntionstring(), CommandType.Text, query);
            return Convert.ToInt32(rowcount);
        }

        /// <summary>
        /// 获取补贴发放部门汇总表
        /// </summary>
        /// <returns></returns>
        public DataSet BindList(int pagesize, int startindex, string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                int startrow = pagesize * (startindex - 1);
                int endrow = pagesize * startindex + 1;
                string query = "select * from(select ROW_NUMBER() OVER(ORDER BY a1.Dep_ID ASC) rownumber,* from (select distinct(a.Dep_ID)," +
                               "a.deptName,Count(c.ID) as BTCount,SUM(c.ConMoney/100.00) as BTMoney from DeptView a " +
                               "left join V_PERSONNEL_REPORT b on a.Dep_ID=b.DEP_ID left join Con_Record c on b.PID=c.PID " +
                               "where c.DealMark in(select DepositTypeID from CON_DEPOSITTYPE where DepositName in('单位补贴')) " +
                                strquery + " group by a.Dep_ID,a.deptName) a1) a2 " +
                               "where a2.rownumber > " + startrow.ToString() + " and a2.rownumber < " + endrow.ToString();
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        public DataSet GetListAll(string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select distinct(a.Dep_ID)," +
                               "a.deptName,Count(c.ID) as BTCount,SUM(c.ConMoney/100.00) as BTMoney from DeptView a " +
                               "left join V_PERSONNEL_REPORT b on a.Dep_ID=b.DEP_ID left join Con_Record c on b.PID=c.PID " +
                               "where c.DealMark in(select DepositTypeID from CON_DEPOSITTYPE where DepositName in('单位补贴')) " +
                               strquery + " group by a.Dep_ID,a.deptName order by a.Dep_ID";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }

        /// <summary>
        /// 计算补贴人数与补贴总金额的合计
        /// </summary>
        /// <param name="strquery"></param>
        /// <returns></returns>
        public DataSet GetSum(string strquery)
        {
            using (SqlConnection conn = new SqlConnection(GetConntionString.conntionstring()))
            {
                sqldb db = new sqldb();
                string query = "select sum(a1.BTCount) as AllCount,sum(a1.BTMoney) as AllMoney from (select distinct(a.Dep_ID)," +
                               "a.deptName,Count(c.ID) as BTCount,SUM(c.ConMoney/100.00) as BTMoney from DeptView a " +
                               "left join V_PERSONNEL_REPORT b on a.Dep_ID=b.DEP_ID left join Con_Record c on b.PID=c.PID " +
                               "where c.DealMark in(select DepositTypeID from CON_DEPOSITTYPE where DepositName in('单位补贴')) " +
                               strquery + " group by a.Dep_ID,a.deptName) a1";
                DataSet ds = db.ExecuteDataset(conn, CommandType.Text, query);
                return ds;
            }
        }
    }
}
